package com.hnkywl.sts.service.organ;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.organ.KhhDao;
import com.hnkywl.sts.entity.organ.Ejzh;
import com.hnkywl.sts.entity.organ.Khh;
import com.hnkywl.sts.entity.organ.Qyjg;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-25
 * Time: 下午3:38
 * To change this template use File | Settings | File Templates.
 */
@Service
public class KhhService {
    private KhhDao khhDao;
    private EjzhService ejzhService;


    /**
     * 查询开户行列表
     *
     * @param nbgsId   内部公司ID
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Transactional
    public String findKhhList(Long nbgsId,String qc,String jc, int pageNo, int pageSize) {
        Page<Khh> khhPage = this.khhDao.findKhhList(nbgsId, qc,jc,pageNo, pageSize);
        String khhJson = BaseUtils.fetchCollectionToJson(khhPage.getResult(), "id", "jc", "qc", "lxdh", "zh", "zhlx.id", "kmdm","drzh","zhlx.z","gysfzhsdm","khfzhsdm","nbgs.id", "nbgs.qc");
        return "{\"total\":" + khhPage.getTotalCount() + ",\"rows\":[" + khhJson + "]}";
    }

    public String findKmdmList(Long nbgsId,int pageNo,int pageSize){
        Page<Khh> khhPage = this.khhDao.findKhhList(nbgsId,null,null, pageNo, pageSize);
        List<Khh> list = khhPage.getResult() != null ? khhPage.getResult() : new ArrayList<Khh>();
        List<Map> grid = new ArrayList<Map>();
        for(Khh khh : list){
            Map item = new HashMap();
            item.put("khh.id",khh.getId());
            item.put("nbgsJc",khh.getNbgs()==null?khh.getQyjg().getJc():khh.getNbgs().getJc());
            item.put("khh.kmdm",khh.getKmdm());
            item.put("khh.gysfzhsdm",khh.getGysfzhsdm());
            item.put("khh.khfzhsdm",khh.getKhfzhsdm());
            item.put("khh.qc",khh.getQc());
            item.put("khh.zh",khh.getZh());
            grid.add(item);
        }
        return "{\"total\":" + khhPage.getTotalCount() + ",\"rows\":" + new Gson().toJson(grid) + "}";
    }

    @Transactional
    public void saveKmdm(Khh khh){
        Khh k = khhDao.get(khh.getId());
        k.setKmdm(khh.getKmdm());
        k.setGysfzhsdm(khh.getGysfzhsdm());
        k.setKhfzhsdm(khh.getKhfzhsdm());
        khhDao.save(k);
    }

    /**
     * 查询开户行列表
     *
     * @param khhmc    开户行名称
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Transactional
    public String findKhhmc(String khhmc,Long nbgsId, int pageNo, int pageSize) {
        Page<Khh> khhPage = this.khhDao.findKhhList(khhmc,nbgsId, pageNo, pageSize);
        List<Khh> khhList = khhPage.getResult() == null ? new ArrayList<Khh>() : khhPage.getResult();
        String khhJson = BaseUtils.fetchCollectionToJson(khhList, "id", "jc","qc");
        return "[" + khhJson + "]";
    }


    /**
     * 查询开户行列表
     *
     * @param
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Transactional
    public String findNbgsKhhmc(Long nbgsId, int pageNo, int pageSize) {
        Page<Khh> khhPage = this.khhDao.findNbgsKhhList(nbgsId, pageNo, pageSize);
        List<Khh> khhList = khhPage.getResult() == null ? new ArrayList<Khh>() : khhPage.getResult();
        String khhJson = BaseUtils.fetchCollectionToJson(khhList, "id", "qc","zh");
        return "[" + khhJson + "]";
    }


    /**
     * 查询开户行
     *
     * @param id
     * @return
     */
    @Transactional
    public Khh findKhhById(Long id) {
        return this.khhDao.get(id);
    }

    /**
     * 保存开户行信息
     *
     * @param khh 开户行实体
     * @return
     */
    @Transactional
    public void saveKhh(Khh khh) {
        khh.setCreateUser(YgService.getCurrentYg());
        khh.setCreateOrgan(YgService.getCurrentYg().getZggs());
        khh.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        khh.setDateCreated(new Date());

        khh.setLastUpdated(khh.getDateCreated());
        this.khhDao.save(khh);
    }

    /**
     * 更新工程信息
     *
     * @param khh 开户行
     * @return
     */
    @Transactional
    public void updateKhh(Khh khh) throws RuntimeException {
        Khh clone = this.khhDao.get(khh.getId());
        clone = (Khh) ReflectionUtils.getInstance(clone, khh);
        clone.setLastUpdated(new Date());
        this.khhDao.save(clone);
    }

    /**
     * 删除开户行信息
     *
     * @param id 开户行ID
     * @return
     */
    @Transactional
    public void deleteKhh(Long id) {
        Khh khh = khhDao.get(id);
        List<Ejzh> ejzhList = ejzhService.findEjzhByKhhId(khh.getId());
        if(ejzhList.size()>0){
            throw new RuntimeException("开户行下存在二级账户不可删除");
        }
        khh.setLastUpdated(new Date());
        khh.setDeleted(1);
        this.khhDao.save(khh);
    }

    /**
     * ajax远程验证
     *
     * @param value 验证值
     * @param field 验证字段
     * @return
     */
    public boolean ajaxValidate(Long id,Long  nbgsId,String value, String field) {
        return khhDao.checkUnique(id, nbgsId,value, field);
    }

    public Khh findKhhByZh(String zh){
        return this.khhDao.findKhhbyZh(zh);
    }

    /**
     * 获取下一个同层次的排序号
     *
     * @param gsjgId 公司机构的Id
     * @return
     */
    public Long findNextPxh(Long gsjgId) {
        return khhDao.findMaxPxh(gsjgId) == null ? 0 : khhDao.findMaxPxh(gsjgId) + 1;
    }

    @Transactional
    public String findKhhByNbgsId(Long gsjgId) {
        List<Map> khhList = this.khhDao.findKhhByNbgsId(gsjgId);
        List<Khh> khhs = new ArrayList<Khh>();
        for (Map m : khhList) {
            Khh k = new Khh();
            k.setId(Long.parseLong(m.get("khh.id").toString()));
            k.setJc(m.get("khh.jc").toString());
            khhs.add(k);
        }
        String qyjgJson = BaseUtils.fetchCollectionToJson(khhs, "id", "jc");
        return "[" + qyjgJson + "]";

    }


    @Transactional
    public String findKhhByEjzhId(Long ejzhId) {
        List<Map> khhList = this.khhDao.findKhhByEjzhId(ejzhId);
        List<Khh> khhs = new ArrayList<Khh>();
        for (Map m : khhList) {
            Khh k = new Khh();
            k.setId(Long.parseLong(m.get("khh.id").toString()));
            k.setJc(m.get("khh.jc").toString());
            khhs.add(k);
        }
        String qyjgJson = BaseUtils.fetchCollectionToJson(khhs, "id", "jc");
        return "[" + qyjgJson + "]";

    }


    @Autowired
    public void setKhhDao(KhhDao khhDao) {
        this.khhDao = khhDao;
    }
    @Autowired
    public void setEjzhService(EjzhService ejzhService) {
        this.ejzhService = ejzhService;
    }
}
